package cn.yuan.org.service.impl;

import cn.yuan.basic.service.impl.BaseServiceImpl;
import cn.yuan.org.domain.Department;
import cn.yuan.org.mapper.DepartmentMapper;
import cn.yuan.org.service.IDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartService{
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void save(Department department) {
        departmentMapper.save(department);//保存之后，拿到自己生成的id
        if(department.getParent()==null){//如果没有上级
            department.setDirPath("/"+department.getId());
        }else{
                department.setDirPath(departmentMapper.loadById(department.getParent().getId()).getDirPath()+"/"+department.getId());
            }
            departmentMapper.update(department);
    }

    @Override
    public void update(Department department) {
        departmentMapper.update(department);
        if(department.getParent()==null){//如果没有上级
            department.setDirPath("/"+department.getId());
        }else{
            department.setDirPath(departmentMapper.loadById(department.getParent().getId()).getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }


    /*批量删除*/
    @Override
    public void batchDel(Long[] ids) {
        departmentMapper.batchDel(ids);
    }

    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }


}
